package com.Practice;

import java.util.concurrent.*;

public class Practice02 {
    public static void main(String[] args) {
        ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(
                3,
                5,
                1,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(5)
        );
        MyCallable call = new MyCallable(5);
        Future<String> s1 = poolExecutor.submit(call);
        Future<String> s2 = poolExecutor.submit(new MyCallable(10));
        Future<String> s3 = poolExecutor.submit(new MyCallable(100));
        try {
            System.out.println(s1.get());
            System.out.println(s2.get());
            System.out.println(s3.get());
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        poolExecutor.shutdown();
    }
}
class MyCallable implements Callable<String>{
    private int n;
    public MyCallable(int n){
        this.n =n;
    }
    @Override
    public String call() throws Exception {
        int sum = 0;
        for (int i = 0; i <= n; i++) {
            sum += i;
        }
        return Thread.currentThread().getName() + "计算1-" + n + "的和是：" + sum;
    }
}